package com.xjl.app.service;

import com.xjl.app.EntityDTO.DeviceDTO;
import com.xjl.app.EntityDTO.DevicesCallBackMessageDTO;
import com.xjl.app.EntityDTO.StationDTO;
import com.xjl.app.convert.DeviceCallBackMessageDTODOConcert;
import com.xjl.app.domain.EntityDO.DevicesCallBackMessageDO;
import com.xjl.app.domain.service.DeviceCallBackMessageDOService;
import com.xjl.app.infra.entity.DevicesCallBackMessage;
import com.xjl.app.infra.service.DevicesCallBackMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceCallBackMessageDTOService {

    private final DeviceCallBackMessageDOService deviceCallBackMessageDOService;
    private final DeviceCallBackMessageDTODOConcert dtodoInstance = DeviceCallBackMessageDTODOConcert.INSTANCE;
    private final DevicesCallBackMessageService devicesCallBackMessageService;
    private final DeviceDTOService deviceDTOService;
    private final StationDTOService stationDTOService;
    private final ThreadPoolExecutor mQMessageThreadPool;

    public boolean callback(DevicesCallBackMessageDTO messageDTO) {
        DevicesCallBackMessageDO devicesCallBackMessageDO = dtodoInstance.deviceDTOtoDeviceDO(messageDTO);
        DeviceDTO deviceDTO =deviceDTOService.getDeviceById(messageDTO.getDeviceId());
        StationDTO stationDTO =stationDTOService.getStationById(messageDTO.getStationId());
        if(deviceDTO != null&& stationDTO!=null){
            return deviceCallBackMessageDOService.callback(devicesCallBackMessageDO);
        }
        log.error("callback 站点或者设备不存在! stationId ={},deviceId = {}",messageDTO.getDeviceId(),messageDTO.getDeviceId());
        return false;
    }

    public DevicesCallBackMessage save(DevicesCallBackMessageDTO messageDTO) {
        DevicesCallBackMessage message = dtodoInstance.deviceDTOtoDevice(messageDTO);
        if(devicesCallBackMessageService.save(message)){
            return message;
        }
        return null;
    }
}
